from django.db import models

from users.models import User


class Category(models.Model):
    name = models.CharField(max_length=100,verbose_name='新闻类别')
    createDate = models.DateTimeField(auto_now_add=True,verbose_name='创建时间')
    def __str__(self):
        return self.name
    class Meta:
        db_table='news_category'
        verbose_name='新闻分类'
        verbose_name_plural='新闻分类'

class News(models.Model):
    title=models.CharField(max_length=100,verbose_name='新闻标题')
    content=models.TextField(verbose_name='新闻内容')
    createDate = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    # 创建外键, related_name：关系名称，外键表通过该名称查找子表数据，默认News_set
    category=models.ForeignKey(to='Category',related_name='news',on_delete=models.RESTRICT,verbose_name='类别')
    img = models.ImageField(upload_to='news_imgs', verbose_name='图片')
    def __str__(self):
        return self.title
    class Meta:
        db_table = 'news_detail'
        verbose_name = '新闻列表'
        verbose_name_plural = '新闻列表'

class Comment(models.Model):
    #评论的新闻
    new=models.ForeignKey(to=News,related_name='comments',on_delete=models.CASCADE,verbose_name='新闻')
    #评论用户
    user = models.ForeignKey(to=User,related_name='comments',on_delete=models.CASCADE,verbose_name='用户')
    # 评论内容
    content = models.TextField(verbose_name='评论')
    createDate = models.DateTimeField(auto_now_add=True,verbose_name='创建时间')
    class Meta:
        db_table='news_comment'
        verbose_name = '新闻评论'
        verbose_name_plural='新闻评论'




